//
//  AlbumHomeViewController.swift
//  GifUsage
//
//  Created by han on 2021/4/14.
//  Copyright © 2021 Ting Han. All rights reserved.
//

import UIKit

class AlbumHomeViewController: UITableViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "NativePhoto" { // 调用系统相册
            
        } else if segue.identifier == "AlbumList" { // 相簿列表
            
        }
    }
}

extension AlbumHomeViewController {
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        switch indexPath.row {
        case 0: // 调用系统相册选择器
            performSegue(withIdentifier: "NativePhoto", sender: nil)
        case 1: // 相簿列表
            pushToAlbumList()
        case 2: // 我的相册（所有图片视频）
            pushToAlbum()
        case 3: // 我的照片
            pushToAlbum(.image)
        case 4: // 我的视频
            pushToAlbum(.video)
        case 5: // 我的LivePhoto
            pushToAlbum(.livePhoto)
        case 6: // 我的连拍
            pushToAlbum(.burst)
        case 7: // 我的GIF
            pushToAlbum(.gif)
        default:
            break
        }
    }
    
    func pushToAlbumList() {
        func pushAction(_ result: Bool?) {
            if result == false {
                showTips(self, "", "没有权限")
                return
            } else if result == true {
                performSegue(withIdentifier: "AlbumList", sender: nil)
            }
        }
        let result = GIFPickerManager.shared.isAuthorized { (result) in
            pushAction(result)
        }
        pushAction(result)
    }
    
    func pushToAlbum(_ assetType: AssetCollectionType? = nil) {
        func pushAction(_ result: Bool?) {
            if result == false {
                showTips(self, "", "没有权限")
                return
            } else if result == true {
                let grid = AssetGridViewController()
                if let assetType = assetType {
                    let result = GIFPickerManager.shared.fetchAsset(assetType)
                    grid.fetchResult = result
                }
                navigationController?.pushViewController(grid, animated: true)
            }
        }
        
        let result = GIFPickerManager.shared.isAuthorized { (result) in
            pushAction(result)
        }
        pushAction(result)
    }
}
